package com.fluagen.example.vertx.templates;

import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

public class Runner {

	private static Logger log = LoggerFactory.getLogger(Runner.class.getName());

	public static Future<Vertx> deploy(Class<?> cls) {

		Future<Vertx> future = Future.future();

		Vertx vertx = Vertx.vertx();
		DeploymentOptions options = new DeploymentOptions();
		vertx.deployVerticle(cls.getName(), options, v -> {
			if (v.succeeded()) {
				log.info("Verticle deployed");
				future.complete();
			} else {
				log.info("Error while deploying a verticle: " + v.cause().getMessage());
				future.fail(v.cause());
			}
		});

		return future;
	}

	public static Future<Vertx> deploy(Class<?> cls, DeploymentOptions options) {

		Future<Vertx> future = Future.future();

		Vertx vertx = Vertx.vertx();
		vertx.deployVerticle(cls.getName(), options, v -> {
			if (v.succeeded()) {
				log.info("Verticle deployed");
				future.complete();
			} else {
				log.info("Error while deploying a verticle: " + v.cause().getMessage());
				future.fail(v.cause());
			}
		});

		return future;
	}
}